<!DOCTYPE html>
<html lang="zh-CN"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>提现</title>
    <meta name="keywords" content="提现"/>
    <meta name="description" content="提现"/>
    <meta name="viewport"
          content="width=device-width,initial-scale=1, maximum-scale=1,minimum-scale=1, user-scalable=no">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="format-detection" content="telephone=no"/>
    <meta name="viewport" content="width=device-width,viewport-fit=cover">
    <link th:href="@{/shoppingApi/css/public.css}" rel="stylesheet">
    <link th:href="@{/shoppingApi/css/publicOfuser.css}" rel="stylesheet">
    <link th:href="@{/shoppingApi/css/combtn.css}" rel="stylesheet">
    <link th:href="@{/shoppingApi/css/accsetting.css}" rel="stylesheet">
    <link th:href="@{/shoppingApi/css/weui.min.css}" rel="stylesheet">
    <link th:href="@{/shoppingApi/css/jquery-weui.min.css}" rel="stylesheet">
    <meta name="_csrf" th:content="${_csrf.token}"/>
    <meta name="_csrf_header" th:content="${_csrf.headerName}"/>
</head>
<body>

<div class="main accsetting">

    <div class="main_con c323232 withdraw">

        <div class="userinfo title">
            <span>提现</span>
            <input type="hidden" id="msg" name="msg" th:if="${map!=null}" th:value="${map.msg}">
        </div>

        <div class="mt10 pl15 pr15 height44 bgfff flex flex-pj flex-ac item">
            <label for="name" class="ft16">收款人</label>
            <div><input id="name" class="ft16" type="text"></div>
            <input type="hidden" class="userno-no" name="userNo" value=""/>
        </div>

        <div class="mt1 pl15 pr15 height44 bgfff flex flex-pj flex-ac item">
            <label for="creditcard" class="ft16">银行卡号</label>
            <div><input id="creditcard" class="ft16" type="text"></div>
        </div>

        <div class="mt1 pl15 pr15 height44 bgfff flex flex-pj flex-ac item">
            <label for="place" class="ft16">开户行</label>
            <div><input id="place" class="ft16" type="text"></div>
        </div>
        <div class="mt30 mb10 pl15 tips"><span class="user-no"></span>用户可提现积分（元）<span id="max">0.00</span>
        </div>

        <div class="mt1 pl15 pr15 pt10 pb10 bgfff input">
            <label for="total" class="ft14">提现积分</label>
            <div><input id="total" class="ft18" type="text" placeholder="￥0.00"></div>
        </div>

        <div class="pl5 pr15 mt40"><a class="submit  button red bigrounded">下一步</a></div>
    </div>

</div>
<div class="weui-mask-custom"></div>
<div id="password" class="weui-dialog-custom">
    <div class="weui-dialog__hd">
        <strong class="weui-dialog__title">输入密码</strong>
    </div>
    <div class="weui-dialog__bd">
        <div class="passwordarea">
            <ul class="passwordgrid">
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
                <li></li>
            </ul>
            <input class="passwordinput" id="passwordinput" type="password" maxlength="6">
        </div>
    </div>
    <div class="weui-dialog__ft">
        <a href="javascript:;" class="weui-dialog__btn default" id="cancel">取消</a>
        <a href="javascript:;" class="weui-dialog__btn primary" id="confirm">确定</a>
    </div>
</div>
</body>
<script th:src="@{/shoppingApi/js/jquery-3.3.1.min.js}"></script>
<script th:src="@{/shoppingApi/js/jquery-weui.min.js}"></script>
<script>
    $(function () {
        // 若无设置支付密码，则前往设之
        var msg = $('#msg').val();
        var flags = true;
        if (msg == '1') {
            flags = false;
        }
        if (flags) {
            $.alert("积分取现需设置支付密码，请前往设置！", "提示", function () {
                //点击确认后的回调函数
                var redirect = encodeURIComponent('/wap/openWithDrawals');
                location.href = '/wap/updatePayPwd?redirect=' + redirect;
            });
        }
        if (navigator.userAgent.indexOf("Html5Plus") > -1) {
            $('.userinfo.title').hide();
        }
        $('#name').focus();
        $.toast.prototype.defaults.duration = 1000;
        $('.submit').on('click', function () {
            var name = $('#name').val();
            var creditcard = $('#creditcard').val();
            var place = $('#place').val();
            var total = $('#total').val();
            var max = Number($('#max').text())
            if (!name) {
                $.toast('请输入收款人', 'text');
                return;
            }
            if (!creditcard) {
                $.toast('请输入银行卡号', 'text');
                return;
            }
            if (!place) {
                $.toast('请输入开户行', 'text');
                return;
            }
            if (!total) {
                $.toast('请输入提现积分', 'text');
                return;
            }
            if (Number(total) > max) {
                $.toast('可提现积分不足', 'text');
                return;
            }
            $.confirm("积分提现将扣除5%的手续费", '确定提现？', function () {
                //点击确认后的回调函数
                // 输入密码
                $('#password').addClass('weui-dialog--visible').siblings('.weui-mask-custom').addClass('weui-mask--visible')
                $('#passwordinput').focus();
            }, function () {
                //点击取消后的回调函数
                $.toast('已取消提现', 'cancel')
                $('#passwordinput').val('')
            });

        });

        // 输入密码后确认
        $(document).on('click', '#confirm', function () {
            var password = $('#passwordinput').val();
            var userNo = $('[name=userNo]').val();
            if (!password || password.length < 6) {
                return;
            }
            flag = valPayPwd(password);
            if (flag) {
                flag = confireWithdrawals();
                if (flag) {
                    $.toast('提现成功');
                  $.ajax({
                      url:'/wap/mywallet?userNo='+userNo,
                      success:function (res) {
                          window.location.href='/wap/mywallet?userNo='+userNo;
                      }
                  })

                } else {
                    $.toast('提交数据有误！', 'cancel');
                }

            } else {
                $.toast('支付密码错误！', 'cancel');
            }

            hidePasswordPop();
        })
        // 输入密码后取消
        $(document).on('click', '#cancel', function () {
            $.toast('您取消了提现', 'cancel');
            hidePasswordPop()
        })

        function hidePasswordPop() {
            $('#password').removeClass('weui-dialog--visible').siblings('.weui-mask-custom').removeClass('weui-mask--visible')
            $('#passwordinput').val('');
        }
    })

    getWithdrawalsData();

    function getWithdrawalsData() {
        $.ajax({
            url: '/wap/openChangeFinace',
            success: function (res) {
                $('.user-no').html(res.user_no);
                if (res.finaceTotal != null) {
                    $("#max").html(res.finaceTotal.balance);
                }
                if (res.userNo != null) {
                    $(".userno-no").val(res.userNo.userNo);
                }
            },
            error: function (res) {
                $.toast('页面有误！', 'cancel')
            }
        })
    }

    /*确认支付密码*/
    var flag = false;

    function valPayPwd(payPwd) {
        $.ajax({
            type: 'post',
            url: '/wap/valPayPwd',
            data: {
                payPwd: payPwd
            },
            async: false,
            success: function (res) {
                flag = res.flag;
            }
        })
        return flag;
    }

    /*确认密码后提交提现数据*/
    function confireWithdrawals() {
        var name = $('#name').val();//收款人
        var creditcard = $('#creditcard').val();//卡号
        var place = $('#place').val();//开户行
        var total = $('#total').val();//
        $.ajax({
            type: 'post',
            url: '/wap/withdrawals',
            data: {
                username: name,
                account: creditcard,
                bankName: place,
                cashCount: total
            },
            success: function (res) {
                console.log(res);
                if (res.msg == '1') {
                    flag = true;
                }
            }
        })
        return flag;

    }


</script>
</html>